//package com.ruoyi.framework.config;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
//import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
//import org.springframework.boot.web.server.WebServerFactoryCustomizer;
//import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.event.ContextClosedEvent;
//
//import java.util.concurrent.Executor;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author liutao
// * @version 1.0
// * @description: TODO
// * @date 2024/3/14 13:38
// */
//@Configuration
//public class TomcatConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
//    @Override
//    public void customize(ConfigurableServletWebServerFactory factory) {
//        TomcatServletWebServerFactory serverFactory = (TomcatServletWebServerFactory) factory;
//        serverFactory.setProtocol("org.apache.coyote.http11.Http11Nio2Protocol");
//        serverFactory.addConnectorCustomizers(c -> {
//            Http11Nio2Protocol protocol = (Http11Nio2Protocol) c.getProtocolHandler();
//            //定制化keepalivetimeout,设置30秒内没有请求则服务端自动断开keepalive链接
//            protocol.setKeepAliveTimeout(30000);
//            //当客户端发送超过10000个请求则自动断开keepalive链接
//            protocol.setMaxKeepAliveRequests(10000);
//        });
//        serverFactory.addConnectorCustomizers(gracefulShutdown());
//    }
//    /**
//     * 用于接受 shutdown 事件
//     */
//    @Bean
//    public GracefulShutdown gracefulShutdown() {
//        return new GracefulShutdown();
//    }
//    /**
//     * 优雅关闭 Spring Boot。容器必须是 tomcat
//     */
//    private class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
//        private final Logger log = LoggerFactory.getLogger(GracefulShutdown.class);
//        private volatile Connector connector;
//        private final int waitTime = 10;
//
//        @Override
//        public void customize(Connector connector) {
//            this.connector = connector;
//        }
//
//        @Override
//        public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
//            this.connector.pause();
//            Executor executor = this.connector.getProtocolHandler().getExecutor();
//            if (executor instanceof ThreadPoolExecutor) {
//                try {
//                    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
//                    threadPoolExecutor.shutdown();
//                    if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
//                        log.warn("Tomcat 进程在 {} 秒内无法结束，尝试强制结束", waitTime);
//                    }
//                } catch (InterruptedException ex) {
//                    Thread.currentThread().interrupt();
//                }
//            }
//        }
//    }
//}
